<html>
<head>
<link rel="stylesheet" href="../../js/sh/SyntaxHighlighter.css" type="text/css" />
<script src="../../js/sh/shCore.js"></script>
<script src="../../js/sh/shBrushJava.js"></script>
<style>

* {
font-family:Courier New,monospace;
  padding: 0;
  margin: 0;
  white-space: nowrap;
  font-size: 11px;
}

.dp-highlighter {
  white-space: nowrap;
  overflow: visible;
  width: 700px;
  font-size: 11px;
  font-family:Courier New,monospace;
}
</style>
</head>
<body>
<textarea name="code" class="java:nogutter:nocontrols" rows="15" cols="120">
/*
 * GWT-Ext Widget Library
 * Copyright 2007 - 2008, GWT-Ext LLC., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 3 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
 
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.ui.RootPanel;
import com.gwtext.client.core.Connection;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.dd.DragData;
import com.gwtext.client.dd.DragDrop;
import com.gwtext.client.dd.DragSource;
import com.gwtext.client.dd.DropTarget;
import com.gwtext.client.dd.DropTargetConfig;
import com.gwtext.client.util.Format;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.form.FieldSet;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.Radio;
import com.gwtext.client.widgets.grid.*;
import com.gwtext.client.widgets.layout.HorizontalLayout;
import com.gwtext.client.widgets.layout.VerticalLayout;
import com.gwtext.client.widgets.tree.*;
import com.gwtext.client.widgets.tree.event.TreePanelListenerAdapter;

public class GridTreeDDSample implements EntryPoint {

    public void onModuleLoad() {
        Panel panel = new Panel();
        panel.setBorder(false);
        panel.setPaddings(15);

        //create form for nody drop style
        FormPanel formPanel = new FormPanel();
        formPanel.setHideLabels(true);

        FieldSet fieldSet = new FieldSet("Drop style");
        fieldSet.setWidth(420);
        formPanel.add(fieldSet);

        final Radio moveRadio = new Radio();
        moveRadio.setName("dropstyle");
        moveRadio.setBoxLabel("Move");
        moveRadio.setChecked(true);
        fieldSet.add(moveRadio);

        final Radio copyRadio = new Radio();
        copyRadio.setName("dropstyle");
        copyRadio.setBoxLabel("Copy");
        fieldSet.add(copyRadio);

        //create source countries grid
        final Store store = new SimpleStore(new String[]{"abbr", "country"}, getCountries());
        store.load();

        final Store store2 = new SimpleStore(new String[]{"abbr", "country"}, new String[][]{});
        store.load();
        
        ColumnConfig[] columns = {
                new ColumnConfig("Flag", "abbr", 45, true, new Renderer() {
                    public String render(Object value, CellMetadata cellMetadata,
                                         Record record, int rowIndex, int colNum, Store store) {
                        return Format.format("<img src=\"images/flags/{0}.gif\">",
                                new String[]{record.getAsString("abbr")});
                    }
                }, "abbr"),
                new ColumnConfig("Code", "abbr", 45),
                new ColumnConfig("Country", "country", 90, true, null, "country")
        };

        ColumnModel columnModel = new ColumnModel(columns);
        final GridPanel countriesGrid = new GridPanel();
        countriesGrid.setTitle("Countries");
        countriesGrid.setStore(store);
        countriesGrid.setColumnModel(columnModel);
        countriesGrid.setHeight(390);
        countriesGrid.setWidth(200);
        countriesGrid.setAutoExpandColumn("country");
        countriesGrid.setEnableDragDrop(true);
        countriesGrid.setDdGroup("myDDGroup");

        //create target vacation tree
        final TreePanel tripTreePanel = new TreePanel();
        tripTreePanel.setTitle("Trip Planner");
        tripTreePanel.setAnimate(true);
        tripTreePanel.setEnableDrop(true);
        tripTreePanel.setDdGroup("myDDGroup");
        tripTreePanel.setContainerScroll(true);
        tripTreePanel.setRootVisible(true);
        tripTreePanel.setWidth(200);
        tripTreePanel.setHeight(390);
        tripTreePanel.setEnableDD(true);

        final XMLTreeLoader tripLoader = new XMLTreeLoader();
        tripLoader.setDataUrl("data/trip.xml");
        tripLoader.setMethod(Connection.GET);
        tripLoader.setRootTag("vacations");
        tripLoader.setFolderIdMapping("@title");
        tripLoader.setFolderTag("trip");
        tripLoader.setQtipMapping("@qtip");
        tripLoader.setIconMapping("@icon");
        tripLoader.setAttributeMappings(new String[]{"@trip"});

        final AsyncTreeNode tripRoot = new AsyncTreeNode("Places to Visit", tripLoader);
        tripTreePanel.setRootNode(tripRoot);
        tripRoot.expand();
        tripTreePanel.expandAll();

        //add trip tree listener that handles move / copy logic
        tripTreePanel.addListener(new TreePanelListenerAdapter() {
            public boolean doBeforeNodeDrop(TreePanel treePanel, TreeNode target, DragData dragData,
                                            String point, DragDrop source, TreeNode dropNode,
                                            DropNodeCallback dropDropNodeCallback) {
                if (dragData instanceof GridDragData) {
                    GridDragData gridDragData = (GridDragData) dragData;
                    Record[] records = gridDragData.getSelections();
                    TreeNode[] copyNodes = new TreeNode[records.length];
                    for (int i = 0; i < records.length; i++) {
                        Record record = records[i];
                        String country = record.getAsString("country");
                        TreeNode copyNode = new TreeNode(country);
                        copyNode.setId(country);
                        copyNode.setIcon("images/flags/" + record.getAsString("abbr") + ".gif");
                        copyNodes[i] = copyNode;
                        target.appendChild(copyNode);
                        if (moveRadio.getValue()) {
                            GridPanel grid = gridDragData.getGrid();
                            Store store = grid.getStore();
                            store.remove(record);
                            store.commitChanges();
                            
                            store2.add(record);
                            store2.commitChanges();
                        }
                    }
                }
                return true;
            }
        });
            
        DropTargetConfig cfg = new DropTargetConfig();
        cfg.setdDdGroup("myDDGroup");
            
        DropTarget tg = new DropTarget(countriesGrid, cfg) {
            public boolean notifyDrop(DragSource source, EventObject e,
                  DragData data) {
           		if(data instanceof GridDragData) return false;
                	
                TreeDragData treeDragData = (TreeDragData) data;
                TreeNode treeNode = treeDragData.getTreeNode();
                String country = treeNode.getText();
                	
                GridView view = countriesGrid.getView();
                int droppedRow = view.findRowIndex(e);
                	
                int index = store2.find("country", country, 0, true, true);
                Record record = store2.getAt(index);
                	
                if(record != null){
                    if (moveRadio.getValue()) {
                      	if(droppedRow == -1){
                       		store.add(record);
                       	}else{
                       		store.insert(droppedRow, record);
                       	}
                        store.commitChanges();
                    }
                     store2.remove(record);
                     store2.commitChanges();
                }
                    
                TreeNode node = tripTreePanel.getNodeById(country);
                tripRoot.removeChild(node);
                return true;
            }

            public String notifyOver(DragSource source, EventObject e,
                  DragData data) {
               return "x-dd-drop-ok";
            }
        };
	
	
        Panel horizontalPanel = new Panel();
        horizontalPanel.setLayout(new HorizontalLayout(25));
        horizontalPanel.add(countriesGrid);
        horizontalPanel.add(tripTreePanel);

        Panel verticalPanel = new Panel();
        verticalPanel.setLayout(new VerticalLayout(15));

        verticalPanel.add(fieldSet);
        verticalPanel.add(horizontalPanel);

        panel.add(verticalPanel);

        RootPanel.get().add(panel);
    }

    private Object[][] getCountries() {
        return new Object[][]{
                new Object[]{"au", "Australia", "Canberra", "Australia",
                        new Integer(18090000), new Integer(7713360)},
                new Object[]{"br", "Brazil", "Brasilia", "South America",
                        new Integer(170000000), new Integer(8547404)},
                new Object[]{"ca", "Canada", "Ottawa", "North America",
                        new Integer(31000000), new Integer(9976140)},
                new Object[]{"cn", "China", "Beijing", "Asia",
                        new Integer(1222017000), new Integer(9596960)},
                new Object[]{"de", "Germany", "Berlin", "Europe",
                        new Integer(80942000), new Integer(356910)},
                new Object[]{"fr", "France", "Paris", "Europe",
                        new Integer(57571000), new Integer(551500)},
                new Object[]{"in", "India", "New Delhi", "Asia",
                        new Integer(913747000), new Integer(3287590)},
                new Object[]{"sc", "Seychelles", "Victoria", "Africa",
                        new Integer(73000), new Integer(280)},
                new Object[]{"us", "United States", "Washington, DC", "North America",
                        new Integer(260513000), new Integer(9372610)},
                new Object[]{"jp", "Japan", "Tokyo", "Asia",
                        new Integer(125422000), new Integer(377800)},
                new Object[]{"ie", "Italy", "Rome", "Eorope",
                        new Integer(57867000), new Integer(301270)},
                new Object[]{"gh", "Ghana", "Accra", "Africa",
                        new Integer(16944000), new Integer(238540)},
                new Object[]{"ie", "Iceland", "Reykjavik", "Europe",
                        new Integer(270000), new Integer(103000)},
                new Object[]{"fi", "Finland", "Helsinki", "Europe",
                        new Integer(5033000), new Integer(338130)},
                new Object[]{"ch", "Switzerland", "Berne", "Europe",
                        new Integer(6910000), new Integer(41290)}
        };
    }
}
</textarea>
<script class="javascript">
dp.SyntaxHighlighter.HighlightAll('code');
</script>
</body>
</html>
